/**
 * 详情对话框
 */
var HxBasicGoodsInfoDlg = {
    data: {
        goodsCustomerId: "",
        goodsCustomerName: "",
        goodsName: "",
        goodsNumber: "",
        goodsPcode69: "",
        goodsXcode69: "",
        goodsHcode69: "",
        goodsHelpcode: "",
        goodsHeavybubble: "",

        goodsSpecs: "",
        goodsBigtype: "",
        goodsBrand: "",
        jibenUnit: "",
        kcUnit: "",
        kcRate: "",
        cgUnit: "",
        cgRate: "",
        pfUnit: "",
        pfRate: "",
        psUnit: "",
        psRate: "",
        anquanNum: "",
        goodsBottom: "",
        goodsLamination: "",
        goodsTraylayer: "",
        goodsSupportvolume: "",
        length: "",
        height: "",
        width: "",
        goodsWeight: "",
        goodsVolume: "",
        goodsCharging: "",
        goodsWarranty: "",
        goodsNodes: "",
        createTime: "",
        updateTime: "",
        createName: "",
        updateName: "",
        deptId: "",
        deliverId: "",
        v1: "",
        v2: "",
        v3: "",
        v4: "",
        v5: "",
        dismantlingParts: "",
        goodsXlbId:"",
    }
};
var DictInfoDlg = {
    data: {
        dictId: "",
        name: ""
    }
};

layui.use(['form', 'admin', 'ax', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/hxBasicGoods/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('hxBasicGoodsForm', result.data);
    if(result.data.v1 != null && result.data.v1 != ""){
        $('#avatarPreview').attr('src', '/system/previewa/' + result.data.v1);
    }

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/hxBasicGoods/editItem", function (data) {
        	if (data.success) {
                Feng.success("更新成功！");
            	window.location.href = Feng.ctxPath + '/hxBasicGoods'
			} else {
                Feng.error("更新失败！"+data.message);
			}
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    
    upload.render({
        elem: '#imgHead'
        , url: Feng.ctxPath + '/system/upload'
        , before: function (obj) {
            obj.preview(function (index, file, result) {
                $('#avatarPreview').attr('src', result);
            });
        }
        , done: function (res) {
            $('#v1').val(res.data.fileId);
        }
        , error: function () {
            Feng.error("上传失败！");
        }
    });

	function getCaption (obj) {//截取指定字符后的字符串
	    var index = obj.lastIndexOf("|")+1
	    var res = obj.substring(index, obj.length)
	    return res
	}

    //父级字典时
    $('#goodsBigtype').click(function () {
        var formName = encodeURIComponent("parent.DictInfoDlg.data.name");
        var formId = encodeURIComponent("parent.DictInfoDlg.data.dictId");
        var treeUrl = encodeURIComponent("/hxBasicGoodsCode/treeo");

        layer.open({
            type: 2,
            title: '所属分类',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#goodsBigtypedictId").val(DictInfoDlg.data.dictId);
                $("#goodsBigtype").val(getCaption(DictInfoDlg.data.name));
            }
        });
    });

    //输入长时
    $("#length").keyup(function () {
        var length = $("#length").val();//应入长
        var width = $("#width").val();//宽
        var height = $("#height").val();//高
		if(!isNaN(length) && length != "" && parseFloat(length) > 0){
			if(!isNaN(width) && width != "" && parseFloat(width) > 0){
				if(!isNaN(height) && height != "" && parseFloat(height) > 0){
	        		$("#goodsVolume").val((length * width * height).toFixed(4));//改变体积
				}
			}
		}
    });

    //输入宽时
    $("#width").keyup(function () {
        var width = $("#width").val();//宽
        var length = $("#length").val();//应入长
        var height = $("#height").val();//高
		if(!isNaN(width) && width != "" && parseFloat(width) > 0){
			if(!isNaN(length) && length != "" && parseFloat(length) > 0){
				if(!isNaN(height) && height != "" && parseFloat(height) > 0){
	        		$("#goodsVolume").val((length * width * height).toFixed(4));//改变体积
				}
			}
		}
    });

    //输入高时
    $("#height").keyup(function () {
        var height = $("#height").val();//高
        var width = $("#width").val();//宽
        var length = $("#length").val();//应入长
		if(!isNaN(height) && height != "" && parseFloat(height) > 0){
			if(!isNaN(width) && width != "" && parseFloat(width) > 0){
				if(!isNaN(length) && length != "" && parseFloat(length) > 0){
	        		$("#goodsVolume").val((length * width * height).toFixed(4));//改变体积
				}
			}
		}
    });

    //输入底时
    $("#goodsBottom").keyup(function () {
        var goodsBottom = $("#goodsBottom").val();//底
        var goodsLamination = $("#goodsLamination").val();//叠层
		if(!isNaN(goodsBottom) && goodsBottom != "" && parseFloat(goodsBottom) > 0){
    		$("#anquanNum").val(goodsBottom * 2);//改变安全库存值
			if(!isNaN(goodsLamination) && goodsLamination != "" && parseFloat(goodsLamination) > 0){
        		$("#goodsSupportvolume").val(goodsBottom * goodsLamination);//改变每托存量
			}
		}
    });

    //输入叠层
    $("#goodsLamination").keyup(function () {
        var goodsLamination = $("#goodsLamination").val();//叠层
        var goodsBottom = $("#goodsBottom").val();//底
		if(!isNaN(goodsLamination) && goodsLamination != "" && parseFloat(goodsLamination) > 0){
			if(!isNaN(goodsBottom) && goodsBottom != "" && parseFloat(goodsBottom) > 0){
        		$("#goodsSupportvolume").val(goodsBottom * goodsLamination);//改变每托存量
			}
		}
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/hxBasicGoods'
    });

});